use rand::Rng;

const BROWSERS: [&str; 117] = [
    "ABrowse",
    "Acoo+Browser",
    "America+Online+Browser",
    "AmigaVoyager",
    "AOL",
    "Arora",
    "Avant+Browser",
    "Beonex",
    "BonEcho",
    "Browzar",
    "Camino",
    "Charon",
    "Cheshire",
    "Chimera",
    "Chrome",
    "ChromePlus",
    "Classilla",
    "CometBird",
    "Comodo_Dragon",
    "Conkeror",
    "Crazy+Browser",
    "Cyberdog",
    "Deepnet+Explorer",
    "DeskBrowse",
    "Dillo",
    "Dooble",
    "Edge",
    "Element+Browser",
    "Elinks",
    "Enigma+Browser",
    "EnigmaFox",
    "Epiphany",
    "Escape",
    "Firebird",
    "Firefox",
    "Fireweb+Navigator",
    "Flock",
    "Fluid",
    "Galaxy",
    "Galeon",
    "GranParadiso",
    "GreenBrowser",
    "Hana",
    "HotJava",
    "IBM+WebExplorer",
    "IBrowse",
    "iCab",
    "Iceape",
    "IceCat",
    "Iceweasel",
    "iNet+Browser",
    "Internet+Explorer",
    "iRider",
    "Iron",
    "K-Meleon",
    "K-Ninja",
    "Kapiko",
    "Kazehakase",
    "Kindle+Browser",
    "KKman",
    "KMLite",
    "Konqueror",
    "LeechCraft",
    "Links",
    "Lobo",
    "lolifox",
    "Lorentz",
    "Lunascape",
    "Lynx",
    "Madfox",
    "Maxthon",
    "Midori",
    "Minefield",
    "Mozilla",
    "myibrow",
    "MyIE2",
    "Namoroka",
    "Navscape",
    "NCSA_Mosaic",
    "NetNewsWire",
    "NetPositive",
    "Netscape",
    "NetSurf",
    "OmniWeb",
    "Opera",
    "Orca",
    "Oregano",
    "osb-browser",
    "Palemoon",
    "Phoenix",
    "Pogo",
    "Prism",
    "QtWeb+Internet+Browser",
    "Rekonq",
    "retawq",
    "RockMelt",
    "Safari",
    "SeaMonkey",
    "Shiira",
    "Shiretoko",
    "Sleipnir",
    "SlimBrowser",
    "Stainless",
    "Sundance",
    "Sunrise",
    "surf",
    "Sylera",
    "Tencent+Traveler",
    "TenFourFox",
    "theWorld+Browser",
    "uzbl",
    "Vimprobable",
    "Vonkeror",
    "w3m",
    "WeltweitimnetzBrowser",
    "WorldWideWeb",
    "Wyzo",
];

const OS: [&str; 34] = [
    "Windows NT 5.1;Win64; x64",
    "Windows NT 6.0;Win64; x64",
    "Windows NT 6.1;Win64; x64",
    "Windows NT 6.2;Win64; x64",
    "Windows NT 6.3;Win64; x64",
    "Windows NT 10.0;Win64; x64",
    "Windows NT 11.0;Win64; x64",
    "Ubuntu 14.04",
    "Ubuntu 16.04",
    "Ubuntu 18.04",
    "Macintosh; U; PPC Mac OS X; it-it",
    "Macintosh; Intel Mac OS X 10_9_5;",
    "Macintosh; Intel Mac OS X 10_10_5",
    "Macintosh; Intel Mac OS X 10_11_6",
    "Macintosh; Intel Mac OS X 10_12_6",
    "Macintosh; Intel Mac OS X 10_13_6",
    "Macintosh; Intel Mac OS X 10_14_6",
    "Macintosh; Intel Mac OS X 10_15_6",
    "Macintosh; Intel Mac OS X 11_0_1",
    "Linux; U; Deepin 15.11",
    "Linux; U; Deepin 15.12",
    "Linux; U; Deepin 20.1",
    "Linux; Android 4.4.2; Nexus 5 Build/KOT49H",
    "Linux; Android 5.0; SM-G900P Build/LRX21T",
    "Linux; Android 6.0; Nexus 6P Build/MMB29P",
    "Linux; Android 7.0; Pixel Build/NRD90M",
    "Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012",
    "Linux; Android 9; Pixel 3 XL Build/PPP3.180510.008",
    "Linux; Android 10; Pixel 4 Build/QD4A.200805.003",
    "iPhone; CPU iPhone OS 9_0 like Mac OS X",
    "iPhone; CPU iPhone OS 11_0 like Mac OS X",
    "iPhone; CPU iPhone OS 12_0 like Mac OS X",
    "iPhone; CPU iPhone OS 13_0 like Mac OS X",
    "iPhone; CPU iPhone OS 14_0 like Mac OS X",
];

pub fn random_user_agent() -> String {
    let mut rng = rand::thread_rng();
    let os = OS[rng.gen_range(0..OS.len())];
    let browser = BROWSERS[rng.gen_range(0..BROWSERS.len())];
    let version = format!(
        "{}.{}.{}.{}",
        rng.gen_range(1..256),
        rng.gen_range(1..256),
        rng.gen_range(1..256),
        rng.gen_range(1..256)
    );
    let device_id: String = (0..8).map(|_| rng.gen_range(0..16).to_string()).collect();
    format!(
        "Mozilla/5.0 ({};rv:{};{}) AppleWebKit/{}.36 (KHTML, like Gecko) {}/{} Safari/{}.5",
        os,
        rng.gen_range(1..256),
        device_id,
        rng.gen_range(500..600),
        browser,
        version,
        rng.gen_range(30..500)
    )
}

#[cfg(test)]
mod tests {
    use crate::random_user_agent;

    #[test]
    fn test_random_user_agent() {
        let ua = random_user_agent();
        println!("random user agent:{}", ua);
    }
}
